/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package ranab.server.ftp.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import ranab.server.ftp.ConnectionService;
import ranab.server.ftp.FtpConfig;
import ranab.server.ftp.SpyConnectionInterface;


/**
 * This panel is used to monitor user activities.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class SpyPanel extends JPanel implements SpyConnectionInterface {
    
    private JTextPane mLogTxt   = null;
    private FtpConfig mConfig   = null;
    private String mstSessionId = null;
    
    private SimpleAttributeSet mReqAttrs = null;
    private SimpleAttributeSet mResAttrs = null;
    
    
    /**
     * Instantiate this dialog box
     */
    public SpyPanel(FtpConfig config, String sessId) {
        mConfig = config;
        mstSessionId = sessId;
        initComponents();
        
        mReqAttrs = new SimpleAttributeSet();
        StyleConstants.setForeground(mReqAttrs, new Color(0xFF, 0x00, 0xFF));
        
        mResAttrs = new SimpleAttributeSet();
        StyleConstants.setForeground(mResAttrs, new Color(0x00, 0x00, 0x8B));
    }
    
    /**
     * Initialize the UI components
     */
    private void initComponents() {
        setLayout(new BorderLayout());
        mLogTxt = new JTextPane();
        mLogTxt.setEditable(false);
        JScrollPane txtPane = new JScrollPane(mLogTxt, 
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(txtPane, BorderLayout.CENTER);
        
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, this);
        }
    }
    
    /**
     * Get connection session id. 
     */
    public String getSessionId() {
        return mstSessionId;
    }
    
    /**
     * Write server response.
     */
    public void response(String msg) {
        Document doc = mLogTxt.getDocument();
        try {
            doc.insertString(doc.getLength(), msg, mResAttrs);
        }
        catch(BadLocationException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Write user request.
     */
    public void request(String msg) {
        Document doc = mLogTxt.getDocument();
        try {
            doc.insertString(doc.getLength(), msg, mReqAttrs);
        }
        catch(BadLocationException ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * Clear log messages
     */
    public void clearLog() {
        mLogTxt.setText("");
    }
    
    /**
     * Close pane
     */
    public void closePane() {
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, null);
        }
        clearLog();
    }
    
    /**
     * Disconnect user
     */
    public void disconnect() {
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, null);
            conService.closeConnection(mstSessionId);
        }
    }
    
}
